/**
 * 
 */
package com.blog.service.impl;

import java.awt.Menu;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.mapper.SysMenuMapper;
import com.blog.mapper.SysRoleMenuMapper;
import com.blog.pojo.SysMenu;
import com.blog.pojo.SysMenuExample;
import com.blog.pojo.SysRole;
import com.blog.pojo.SysRoleMenu;
import com.blog.pojo.SysRoleMenuExample;
import com.blog.service.SysMenuService;
import com.blog.service.SysRoleService;

/**
 * @author zyj
 *
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	/* 
	 *   �����û�����ѯ�û�������Ȩ��
	 */
	@Override
	public Set<String> listSysMenus(String userName) {
		Set<String> result = new HashSet<>();
		List<SysRole> roles = sysRoleService.listSysRoles(userName);
		List<SysRoleMenu> roleMenus = new ArrayList<>();
		for (SysRole role : roles) {
			SysRoleMenuExample example = new SysRoleMenuExample();
			example.createCriteria().andRoleIdEqualTo(role.getId());
			List<SysRoleMenu> rps = sysRoleMenuMapper.selectByExample(example);
			roleMenus.addAll(rps);
		}
		for (SysRoleMenu roleMenu : roleMenus) {
			SysMenu m = sysMenuMapper.selectByPrimaryKey(roleMenu.getId());
			result.add(m.getName());
		}
		return result;
	}

	/* 
	 *  ��ѯϵͳ��������ԴȨ��
	 */
	@Override
	public List<SysMenu> list() {
		SysMenuExample example = new SysMenuExample();
		example.setOrderByClause("id desc");
		return sysMenuMapper.selectByExample(example);
	}

	/* 
	 *  ������ԴȨ��
	 */
	@Override
	public void add(SysMenu menu) {
		sysMenuMapper.insert(menu);
	}

	/* 
	 * ɾ����Դ
	 */
	@Override
	public void delete(Long id) {
		sysMenuMapper.deleteByPrimaryKey(id);
	}

	/* 
	 * ��ȡ��Դ
	 */
	@Override
	public SysMenu get(Long id) {
		return sysMenuMapper.selectByPrimaryKey(id);
	}

	/* 
	 * ������Դ
	 */
	@Override
	public void update(SysMenu menu) {
		sysMenuMapper.updateByPrimaryKeySelective(menu);
	}

	/* 
	 * ĳ����ɫ����Դ
	 */
	@Override
	public List<SysMenu> list(SysRole role) {
		List<SysMenu> result = new ArrayList<>();
		SysRoleMenuExample example = new SysRoleMenuExample();
		example.createCriteria().andRoleIdEqualTo(role.getId());
		List<SysRoleMenu> rms = sysRoleMenuMapper.selectByExample(example);
		for(SysRoleMenu roleMenu:rms) {
			result.add(sysMenuMapper.selectByPrimaryKey(roleMenu.getMenuId()));
		}
		return result;
	}

	/* 
	 * 
	 */
	@Override
	public boolean needInterceptor(String requestURI) {
		List<SysMenu> ms = list();
		for (SysMenu sysMenu : ms) {
			if(sysMenu.getUrl().equals(requestURI)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public Set<String> listMenusURLs(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

}
